package com.example.ck.controller;

import com.example.ck.service.EmailService;
import com.example.ck.utils.NumUtil;
import com.example.ck.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
@CrossOrigin
public class EmailController {
    @Autowired
    private EmailService emailService;

    /**
     * 登录邮箱验证码
     *
     * @param to
     * @return
     */
    @GetMapping("/code")
    private ResultVo sendEmailCode(String to) {
        String subject = "城科实验室申请系统";
        // 生成验证码
        int code = NumUtil.createCode(4);
        String text = "欢迎使用城科实验室申请系统,您当前的登录验证码为" + code;
        boolean b = emailService.SendEmail(to, subject, text);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setSuccess(true);
            vo.setCode(200);
            vo.setData("发送邮件成功,验证码为:"+code);
        } else {
            vo.setSuccess(false);
            vo.setCode(201);
            vo.setData("发送邮件失败");
        }
        return vo;
    }

    @GetMapping("/msg")
    private ResultVo sendEmailMsg(String to) {
        String subject = "城科实验室申请系统";
        // 生成验证码
        String text = "尊敬的用户,您的实验室申请已通过,请及时登录系统查看详情" ;
        boolean b = emailService.SendEmail(to, subject, text);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setSuccess(true);
            vo.setCode(200);
            vo.setData("发送邮件成功");
        } else {
            vo.setSuccess(false);
            vo.setCode(201);
            vo.setData("发送邮件失败");
        }
        return vo;
    }
}
